package com.hy.erp.inventory.service;

import java.util.List ;

import javax.ejb.Stateless ;
import javax.persistence.EntityManager ;
import javax.persistence.PersistenceContext ;

import org.jboss.seam.ScopeType ;
import org.jboss.seam.annotations.Name ;
import org.jboss.seam.annotations.Scope ;

import com.hy.enterprise.architecture.foundation.ArchitectureFoundationException;
import com.hy.enterprise.framework.service.business.AbstractBusinessService;
import com.hy.erp.inventory.dao.interfaces.IWorkingDao;
import com.hy.erp.inventory.pojo.Working;
import com.hy.erp.inventory.pojo.interfaces.IWorking;
import com.hy.erp.inventory.service.interfaces.IWorkingService;

/**
 * <ul>
 * <li>开发作者：汤莉</li>
 * <li>设计日期：2010-10-8；时间：下午02:36:57</li>
 * <li>字段类型：long；字段名称：serialVersionUID</li>
 * <li>设计目的：用户维护服务</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
@Name("workService")
@ Scope ( ScopeType.STATELESS )
@ Stateless
public class WorkingService
						extends
						AbstractBusinessService < IWorking >
																implements
																IWorkingService
{

	private static final long	serialVersionUID	=
														122778171996664974L ;
	
	@PersistenceContext
	private EntityManager entityManager;
	
	/**
	 * 构造函数
	 */
	public WorkingService(){
		super ( );
	}
	@ Override
	public String[] addWork (
								Object[] working )
	{
		if(working==null)
		{
			return null;
		}
		else
		{
			IWorkingDao dao=( IWorkingDao ) this.getDao ( IWorking.class );
			Working[] working2=new Working[working.length];
            for(int i=0;i<working.length;i++)
            {
            	Working working3=( Working ) working[i];
            	working2[i]=working3;
            }
			return dao.persist ( working2 );
		}
	}
	@ Override
	public Integer removeWork (
								String [ ] workingIds )
	{
		if ( ( null == workingIds )
			     || ( workingIds.length == 0 ) )
			{
				return new Integer (
				                     0 ) ;
			}
			else
			{
				IWorkingDao dao=( IWorkingDao ) this.getDao ( IWorking.class );
				return dao.remove ( workingIds );
			}
	}
	@ SuppressWarnings ( "unused" )
	@ Override
	public boolean modifyWork (
								Working working )
	{
		if ( ( null == working ) || ( null == working.getIdentifier ( ) ))
		{
			return true;
		}
		else
		{
			IWorkingDao dao=( IWorkingDao ) this.getDao ( IWorking.class );
			IWorking iWorking= working;
			
			if ( null == iWorking)
			{
				throw new ArchitectureFoundationException (
				                                            "修改用户实体数据时发生错误，所需要修改的实体在数据库中并不存在" ) ;
			}
			return (dao.merge (iWorking)!=null) ;
		}
	}
	@ Override
	public  List < Working > findWorkListById (
	                                           String PersonId )
	{
		if(PersonId!=null)
		{
			String sql="select * from workings where identifier IN(select working_identifier from person_workings where person_identifier="+PersonId+" )";
			@ SuppressWarnings ( "unchecked" )
			List < Working > list=( List < Working > ) entityManager.createNativeQuery ( sql );
			return list;
		}
			
		return null ;
	}
	@ SuppressWarnings ( "unused" )
	@ Override
	public List < IWorking > getWorkingById (
												String [ ] Id )
	{
		if((Id.length!=0)||Id!=null)
		{
			IWorkingDao dao=( IWorkingDao ) this.getDao ( IWorking.class );
			return dao.findByIdentifier ( Id );
		}
		return null ;
	}
	@ Override
	public List < IWorking > getAllWorking ( )
	{
		IWorkingDao dao=( IWorkingDao ) this.getDao ( IWorking.class );
		return dao.find ( ) ;
	}
	@ Override
	public List < IWorking > getWorkingByQueryName (
														String queryName ,
														Object [ ] args )
	{
		IWorkingDao dao=( IWorkingDao ) this.getDao ( IWorking.class );
		return dao.findByNamedQuery ( queryName , args ) ;
	}
	@ Override
	public String [ ] modifyWork (
									Object [ ] objects )
	{
		if (  null == objects ) 
		{
			return null;
		}
		else
		{
			IWorkingDao dao=( IWorkingDao ) this.getDao ( IWorking.class );
			Working[] workings=new Working[objects.length];
            for(int i=0;i<objects.length;i++)
            {
            	Working working3=( Working ) objects[i];
            	workings[i]=working3;
            }
			
			return dao.merge ( workings );
		}
		
	}

	
}
